package training.officeweb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet(urlPatterns="/hello", initParams={@WebInitParam(name="test", value="testValue") } )
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    @Override
    public void init(ServletConfig config) throws ServletException {
    	
    	System.out.println(config.getInitParameter("test"));
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		String userName =request.getParameter("user_name");
		String password = request.getParameter("password");
		
		writer.print("<html>");
		writer.print("<head> ... </head>");
		writer.write("<body>");
		writer.write("<h1>Hello this is from header 1 </h1>");
		writer.write("User Name: " + userName);
		writer.write("</body>");
		writer.write("</html>");
		
		//getServletContext().getRequestDispatcher("login").forward(paramServletRequest, paramServletResponse);
		//response.sendRedirect("http://google.com");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		String userName =request.getParameter("user_name");
		String password = request.getParameter("password");
		writer.write("Hello World from Servlet");
		writer.write("\n");
		writer.write("User Name: " + userName);
	}

}
